home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
User's Choice Windows CD
/
User's Choice Windows CD (CMS Software)(1993).iso
/
win_q_t
/
trem.zip
/
CONNECT.C
< prev
next >
Wrap
Text File
|
1991-05-11
|
5KB
|
178 lines
/************************************************************************
*
* Copyright (c) 1991 Microsoft Corporation. All Rights Reserved.
*
*-----------------------------------------------------------------------
*
* Project: Windows Terminal Example
*
* Module: connect.c
*
* Author: Bryan A. Woodruff (baw)
*
*
* Remarks: This handles the connection to the COM port.
*
* Revisions:
* 01.00.000 5/10/91 baw Wrote it.
*
************************************************************************/
#include "terminal.h"
/************************************************************************
* BOOL OpenConnection( HWND hWnd )
*
* Description:
* Opens communication port specified in the TERMINFO struct.
* It also sets the CommState and notifies the window via
* the fConnected flag in the TERMINFO struct.
*
* Comments:
* 5/ 9/91 baw Wrote this nifty comment block.
*
************************************************************************/
BOOL OpenConnection( HWND hWnd )
{
char szPort[5] ;
BOOL fRetVal ;
HCURSOR hOldCursor, hWaitCursor ;
LOCALHANDLE hTermInfo ;
NPTERMINFO npTermInfo ;
hTermInfo = GetWindowWord( hWnd, GWW_TERMINFO ) ;
if (NULL == (npTermInfo = (NPTERMINFO) LocalLock( hTermInfo )))
return ( FALSE ) ;
// show the hourglass cursor
hWaitCursor = LoadCursor( NULL, IDC_WAIT ) ;
hOldCursor = SetCursor( hWaitCursor ) ;
lstrcpy( szPort, COMPREFIX ) ;
szPort[3] = (char) ('0' + npTermInfo -> bPort) ;
szPort[4] = NULL ;
if ((npTermInfo -> nCid = OpenComm( szPort, RXQUEUE, TXQUEUE )) < 0)
{
LocalUnlock( hTermInfo ) ;
return ( FALSE ) ;
}
fRetVal = SetupConnection( hWnd ) ;
if (fRetVal)
{
npTermInfo -> fConnected = TRUE ;
SetEvent( hWnd, npTermInfo -> nCid, EV_RXCHAR, WM_EVENT_RXCHAR ) ;
SetTerminalFocus( hWnd ) ;
}
else
{
npTermInfo -> fConnected = FALSE ;
CloseComm( npTermInfo -> nCid ) ;
}
// restore cursor
SetCursor( hOldCursor ) ;
LocalUnlock( hTermInfo ) ;
return ( fRetVal ) ;
} /* end of OpenConnection() */
/************************************************************************
* BOOL SetupConnection( HWND hWnd )
*
* Description:
* This routine sets the communication parameters.
*
* Comments:
* 5/ 9/91 baw Wrote it.
*
************************************************************************/
BOOL SetupConnection( HWND hWnd )
{
BOOL fRetVal ;
BYTE bSet ;
DCB dcb ;
LOCALHANDLE hTermInfo ;
NPTERMINFO npTermInfo ;
hTermInfo = GetWindowWord( hWnd, GWW_TERMINFO ) ;
if (NULL == (npTermInfo = (NPTERMINFO) LocalLock( hTermInfo )))
return ( FALSE ) ;
GetCommState( npTermInfo -> nCid, &dcb ) ;
dcb.BaudRate = npTermInfo -> wBaudRate ;
dcb.ByteSize = npTermInfo -> bByteSize ;
dcb.Parity = npTermInfo -> bParity ;
dcb.StopBits = npTermInfo -> bStopBits ;
// setup hardware flow control
bSet = (BYTE) ((npTermInfo -> bFlowCtrl & FC_HARDWARE) != 0) ;
dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = bSet ;
dcb.CtsTimeout = (bSet) ? 30 : 0 ;
dcb.DsrTimeout = (bSet) ? 30 : 0 ;
// setup software flow control
bSet = (BYTE) ((npTermInfo -> bFlowCtrl & FC_SOFTWARE) != 0) ;
dcb.fInX = dcb.fOutX = bSet ;
dcb.XonChar = ASCII_XON ;
dcb.XoffChar = ASCII_XOFF ;
dcb.XonLim = 10 ;
dcb.XoffLim = 10 ;
// other various settings
dcb.fBinary = TRUE ;
dcb.fParity = TRUE ;
fRetVal = !(SetCommState( &dcb ) < 0) ;
LocalUnlock( hTermInfo ) ;
return ( fRetVal ) ;
} /* end of SetupConnection() */
/************************************************************************
* BOOL CloseConnection( HWND hWnd )
*
* Description:
* Closes the connection to the port. Resets the connect flag
* in the TERMINFO struct.
*
* Comments:
* 5/ 9/91 baw Trying to find something new to comment.
*
************************************************************************/
BOOL CloseConnection( HWND hWnd )
{
LOCALHANDLE hTermInfo ;
NPTERMINFO npTermInfo ;
hTermInfo = GetWindowWord( hWnd, GWW_TERMINFO ) ;
if (NULL == (npTermInfo = (NPTERMINFO) LocalLock( hTermInfo )))
return ( FALSE ) ;
KillEvent( hWnd, npTermInfo -> nCid, EV_RXCHAR ) ;
KillTerminalFocus( hWnd ) ;
CloseComm( npTermInfo -> nCid ) ;
npTermInfo -> fConnected = FALSE ;
LocalUnlock( hTermInfo ) ;
return ( TRUE ) ;
} /* end of CloseConnection() */
/************************************************************************
* End of File: connect.c
************************************************************************/